package com.zznode.redsun.commons.sercurity.action;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import com.zznode.csp.domain.commons.persistence.EntityManager;
import com.zznode.redsun.commons.sercurity.model.Permission;
import com.zznode.redsun.commons.utils.AbstractBaseAction;

@Controller
@ParentPackage("json-default")
@Namespace("/system/permission")
public class PermissionAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	private Permission permission;

	@Autowired
	private EntityManager entityManager;

	@Action(value = "index", results = { @Result(name = SUCCESS, location = "index.jsp") })
	public String index() {
		return SUCCESS;
	}

	@Action(value = "list", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_ROWS }) })
	public String list() {
		Collection<Permission> permissions = entityManager.getAll(Permission.class);
		fillJsonGridRows(permissions);
		return SUCCESS;
	}

	@Action(value = "addNew", results = { @Result(name = SUCCESS, location = "edit.jsp") })
	public String addNew() {
		this.permission = new Permission();
		return SUCCESS;
	}

	@Action(value = "save", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_MESSAGE }) })
	public String save() {
		if (null != permission) {
			this.permission.setCode(this.permission.getCode().toUpperCase());
			if (null != this.permission.getId()) {
				entityManager.update(permission);
			} else {
				entityManager.save(permission);
			}
		} else {
			this.setJsonMessage("保存失败，提交数据为空.");
		}
		return SUCCESS;
	}

	@Action(value = "edit", results = { @Result(name = SUCCESS, location = "edit.jsp") })
	public String edit() {
		String id = this.getParameter("id");
		if (StringUtils.hasText(id)) {
			this.permission = entityManager.get(Permission.class, Long.valueOf(id));
		} else {
			this.permission = new Permission();
		}
		return SUCCESS;
	}

	@Action(value = "remove", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_MESSAGE }) })
	public String remove() {
		String id = this.getParameter("id");
		if (StringUtils.hasText(id)) {
			this.entityManager.deleteById(Permission.class, Long.valueOf(id));
		} else {
			this.setJsonMessage("数据删除失败，未指定所要删除的数据。");
		}
		return SUCCESS;
	}

	public Permission getPermission() {
		return permission;
	}

	public void setPermission(Permission permission) {
		this.permission = permission;
	}

}
